package gapidt.plugin.views;

import java.util.ArrayList;
import java.util.List;

import gapidt.analysis.APIDesignAnalysisResult;
import gapidt.analysis.DesignProblem;
import gapidt.common.Locale;
import gapidt.plugin.AnalysisResultsManager;

import org.apache.commons.lang3.ArrayUtils;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class AnalysisResultsContentProvider implements ITreeContentProvider{
	
	
	public AnalysisResultsContentProvider(){
	}
	
	@Override
	public void dispose() {
		
	}

	@Override
	public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
		
	}
	
	@Override
	public Object[] getElements(Object element){
		if(element instanceof AnalysisResultsManager && ((AnalysisResultsManager) element).getResults().size() > 0)
			return ((AnalysisResultsManager) element).getResults().toArray();
		return new String[]{Locale.getMessage("no_analysis_performed")};
	}

	@Override
	public Object[] getChildren(Object parentElement) {
		if(parentElement instanceof APIDesignAnalysisResult){
			APIDesignAnalysisResult<?> result = (APIDesignAnalysisResult<?>) parentElement;
			if(result.getDesignProblemsCount() > 0){
				List<APIDesignAnalysisResult<?>> childResults = new ArrayList<APIDesignAnalysisResult<?>>();
				for(APIDesignAnalysisResult<?> r : result.getChildResults())
					if(r.getDesignProblemsCount() > 0)
						childResults.add(r);
				return ArrayUtils.addAll(result.getDesignProblems().toArray(), childResults.toArray());
			}
		}
	    return new Object[]{};
	}

	@Override
	public Object getParent(Object element) {
		if(element instanceof APIDesignAnalysisResult)
			 return ((APIDesignAnalysisResult<?>) element).getParentAnalysisResult();
		else if(element instanceof DesignProblem)
			 return ((DesignProblem<?>) element).getAnalysisResult();
		return null;
	}

	@Override
	public boolean hasChildren(Object parentElement) {
		if(parentElement instanceof AnalysisResultsManager)
			return true;
		else if(parentElement instanceof APIDesignAnalysisResult)
			return ((APIDesignAnalysisResult<?>) parentElement).getDesignProblemsCount() > 0;
		return false;
	}
}
